package com.sofwin.ruoyi.scyffinal.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.sofwin.ruoyi.scyffinal.pojo.EquipmentAttributes;
import com.sofwin.ruoyi.scyffinal.pojo.EquipmentFacility;
import com.sofwin.ruoyi.scyffinal.pojo.dto.EquipmentFacilityDto;
import com.sofwin.ruoyi.scyffinal.service.EquipmentAttributesService;
import com.sofwin.ruoyi.scyffinal.service.EquipmentFacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/equipmentFacility")
public class EquipmentFacilityController extends BaseController {
    @Autowired
    private EquipmentFacilityService equipmentFacilityService;
    @Autowired
    private EquipmentAttributesService equipmentAttributesService;

    /**
     * 设备设施列表
     * @param equipmentFacility 查询参数
     * @return
     */
    @GetMapping("list")
//    @RequiresPermissions("risk:equipmentFacility:list")
    public TableDataInfo list(EquipmentFacility equipmentFacility){
        startPage();
//        LambdaQueryWrapper<EquipmentFacility> queryWrapper = new LambdaQueryWrapper<>();
//        if (StringUtils.isNotBlank(equipmentFacility.getEquipmentCode())) {
//            queryWrapper.eq(EquipmentFacility::getEquipmentCode, equipmentFacility.getEquipmentCode());
//        }
//        if (StringUtils.isNotBlank(equipmentFacility.getEquipmentName())) {
//            queryWrapper.like(EquipmentFacility::getEquipmentName, equipmentFacility.getEquipmentName());
//        }
//        if (equipmentFacility.getEquipmentCategory()!=null) {
//            queryWrapper.eq(EquipmentFacility::getEquipmentCategory, equipmentFacility.getEquipmentCategory());
//        }
//        if (StringUtils.isNotBlank(equipmentFacility.getSpecificationModel())) {
//            queryWrapper.eq(EquipmentFacility::getSpecificationModel, equipmentFacility.getSpecificationModel());
//        }
//        if (equipmentFacility.getUseDepartmentId() != null) {
//            queryWrapper.eq(EquipmentFacility::getUseDepartmentId, equipmentFacility.getUseDepartmentId());
//        }
//        if (StringUtils.isNotBlank(equipmentFacility.getManufacturer())) {
//            queryWrapper.like(EquipmentFacility::getManufacturer, equipmentFacility.getManufacturer());
//        }
//        if (StringUtils.isNotBlank(equipmentFacility.getEquipmentType())) {
//            queryWrapper.eq(EquipmentFacility::getEquipmentType, equipmentFacility.getEquipmentType());
//        }
//        if (equipmentFacility.getEquipmentStatus()!= null) {
//            queryWrapper.eq(EquipmentFacility::getEquipmentStatus, equipmentFacility.getEquipmentStatus());
//        }
//        List<EquipmentFacility> list = equipmentFacilityService.list(queryWrapper);
        List<EquipmentFacilityDto> list = equipmentFacilityService.getEquipmentFacilityDtoList(equipmentFacility);
        return getDataTable(list);
    }

    /**
     * 通过id查询设备设施
     * @param id 设备设施id
     * @return
     */
    @GetMapping("getById")
    public AjaxResult getById(@RequestParam String id){
        EquipmentFacility equipmentFacility = equipmentFacilityService.getById(id);
        return success(equipmentFacility);
     }

    /**
     * 修改设备设施
     * @param equipmentFacilityDto
     * @return
     */
     @PutMapping("update")
      public AjaxResult update(@RequestBody EquipmentFacilityDto equipmentFacilityDto){
         LambdaQueryWrapper<EquipmentAttributes> queryWrapper = new LambdaQueryWrapper<>();
//         if(equipmentFacilityDto.getAttributesList().size()>0&&equipmentFacilityDto.getAttributesList().get(0).getId()!=null){
             queryWrapper.eq(EquipmentAttributes::getEquipmentFacilityId, equipmentFacilityDto.getId());
             boolean removeAttribute = equipmentAttributesService.remove(queryWrapper);
//         }
         for (EquipmentAttributes equipmentAttributes : equipmentFacilityDto.getAttributesList()) {
             equipmentAttributesService.save(equipmentAttributes);
         }
         return equipmentFacilityService.updateById(equipmentFacilityDto)?success():warn("修改失败");
     }

    /**
     * 添加设备设施
     * @param equipmentFacilityDto
     * @return
     */
     @PostMapping("add")
      public AjaxResult add(@RequestBody EquipmentFacilityDto equipmentFacilityDto){
         LambdaQueryWrapper<EquipmentAttributes> queryWrapper = new LambdaQueryWrapper<>();
//         if(equipmentFacilityDto.getAttributesList().size()>0&&equipmentFacilityDto.getAttributesList().get(0).getId()!=null){
             queryWrapper.eq(EquipmentAttributes::getEquipmentFacilityId, equipmentFacilityDto.getAttributesList().get(0).getEquipmentFacilityId());
             boolean removeAttribute = equipmentAttributesService.remove(queryWrapper);
//         }
         for (EquipmentAttributes equipmentAttributes : equipmentFacilityDto.getAttributesList()) {
             equipmentAttributesService.save(equipmentAttributes);
         }

        return equipmentFacilityService.save(equipmentFacilityDto)?success():warn("添加失败");
     }

    /**
     * 通过id删除设备设施
     * @param id
     * @return
     */
     @DeleteMapping("delete/{id}")
      public AjaxResult delete(@PathVariable List<String> id){
        return equipmentFacilityService.removeByIds(id)?success():warn("删除失败");
     }
     //获取设备属性列表，参数为设备设施表id
     @GetMapping("getEquipmentAttributesList")
     public TableDataInfo getEquipmentAttributesList(Integer facilityId){
         LambdaQueryWrapper<EquipmentAttributes> queryWrapper = new LambdaQueryWrapper<>();
         queryWrapper.eq(EquipmentAttributes::getEquipmentFacilityId, facilityId);
         return getDataTable(equipmentAttributesService.list(queryWrapper));
     }

    /**
     * 通过id查询设备属性
     * @param id
     * @return
     */
     @GetMapping("getAttributeById")
     public AjaxResult getAttributeById(@RequestParam String id){
         return success(equipmentAttributesService.getById(id));
     }
}
